<?php
namespace app\common\model;

use think\Cache;
use think\Model;
use think\session;

class ImgFolder extends CommonModel
{

    /**
     * 获取站点id
     * 优先从session取值
     * 如果不存在，则默认读取第一个站点ID,并且生成session
     * */
    public function site(){
        $site = '';
        if(session('imgSite')){
            $site = session('imgSite');
        }else{
            $model = Model('common/imgSite')->imgSiteCache();
            if($model){
                $siteId = $model[0]['id'];
                session('site',$siteId);
                $site = $siteId;
            }

        }

        return $site;
    }
    /**
     * 拉取数据并且缓存
     * */
    public function folderData(){
        $result = $this->comSelect();
        Cache('imgFolder',$result);
        return $result;
    }

    /**
     * 如果缓存数据不存在，则读取数据
     * */
    public function folderCache(){
        $folder = Cache('imgFolder');
        $result = $folder?$folder:$this->folderData();
        return $result;
    }

    /**
     * 站点基础数据（某站点的子数据）
     * 注意：要针对某个站点的数据进行操作，必须先建立此function
     * */
    public function siteData(){
        // 站点参数
        $site = $this->site();
        $data = $this->folderCache();
        $result='';
        if($data){
            foreach($data as $k=>$v){
                if($v['site'] == $site){
                    $result[] = $v;
                }
            }

        }
        return $result;
    }

    // 建立初始文件夹
    public function defaultFolder(){
        $res = $this->siteData();
        return $res[0]['id'];
    }

    /**
     * 查询子类数据
     * */
    public function folderPidData($pid=0){
        //$data = $this->folderCache();
        $data = $this->siteData();
        $arr = '';
        if($data){
            foreach($data as $k=>$v){
                if($v['pid']==$pid){
                    $arr[$v['id']] = $v;
                    //$arr[$k] = $v;

                }
            }
        }
        return $arr;
    }



    /**
     * xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
     * */

    /**
     * 无限循环 树结构
     * */
    public function treeLoop($pid=0){
        $data = $this->folderPidData($pid);
        if(empty($data)) return false;
        foreach($data as $k=>$v){
            $result[$v['id']] = $v;
            $result[$v['id']]['sub'] = $this->treeLoop($v['id']);
        }

        return $result;
    }

    /**
     * 无限分类  阶梯形式
     * */
    public function tree($pid=0){
        $data = $this->folderPidData($pid);
        if(empty($data))return false;
        foreach($data as $k=>$v){
            $result[$v['id']] = $v;
            //$result[$v['id']] = $this->tree($v['id']);
            $res = $this->tree($v['id']);
            if($res){
                foreach($res as $kk=>$vv){
                    $result[$vv['id']]['id'] = $vv['id'];
                    $result[$vv['id']]['pid'] = $vv['pid'];
                    $result[$vv['id']]['name'] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|- ".$vv['name'];
                }
            }
        }
        return $result;
    }

    // jsTree 分类结构
    public function jsTreeLoop($pid=0){
        $data = $this->folderPidData($pid);
        if(empty($data)) return false;
        foreach($data as $k=>$v){
            $result[$k]['id'] = $v['id'];
            $result[$k]['pid'] = $v['pid'];
            $result[$k]['text'] =$v['name'];

            $children = $this->jsTreeLoop($v['id']);
            if($children)$result[$k]['children'] =$children;

            //$result[$k]['children'] = 1;
            sort($result);

        }

        return $result;
    }

    // 转opt格式
    public function opt($id){
        $data = $this->tree();
        $opt = '';
        foreach($data as $k=>$v){
            if($v['id'] ==$id){
                $opt .="<option value='".$v['id']."'>".$v['name']."</option>";
            }else{
                $opt .="<option value='".$v['id']."'>".$v['name']."</option>";
            }
        }
        return $opt;
    }

    public function s(){
        $user           = model('ImgFolder');
        $data['name'] = '111';
        $user->save($data);
        return $user->id;
    }

    // 物理创建文件夹
    public function mkdirFolder($path=''){

        $dir = iconv("UTF-8", "GBK", UPLOADS_IMAGES.'/'.$path);
        if (!file_exists($dir)){
            mkdir ($dir,0777,true);
            //echo '创建文件夹bookcover成功';
        }
    }




}